package com.softer.chengxin.traning.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.corp.dao.CorpDao;
import com.softer.chengxin.corp.service.impl.ManagerServiceImpl;
import com.softer.chengxin.file.dao.FileDao;
import com.softer.chengxin.person.domin.Person;
import com.softer.chengxin.traning.dao.TraningInfoDao;
import com.softer.chengxin.traning.entity.TraningInfo;
import com.softer.chengxin.traning.service.TraningInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/1/19
 */
@Service
public class TraningInfoServiceImpl implements TraningInfoService {
    @Autowired
    private TraningInfoDao traningInfoDao;
    @Autowired
    private CorpDao corpDao;
    @Autowired
    private ManagerServiceImpl managerService;
    @Autowired
    private FileDao fileDao;
    /**
     * 通过项目id查询
     *
     * @param projectId
     * @return
     */
    @Override
    public List<Person> selectByProject(String projectId) {
        return traningInfoDao.selectByproject(projectId);
    }

    /**
     * 通过certNo查询有没有用过
     *
     * @param certNo
     * @return
     */
    @Override
    public CommonResult selectByCertNo(String certNo) {
        int no = traningInfoDao.selectByCertNo(certNo);
        if (no > NumberEnum.ZERO.getNum()) {
            return CommonResult.response(NumberEnum.ZERO.getNum(), "编号已重复请重新填写");
        }
        return CommonResult.response(NumberEnum.ONE.getNum());
    }

    @Override
    public PageInfo<TraningInfo> selectAllTraning(Page page, TraningInfo traningInfo) {
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<TraningInfo> traningInfos = traningInfoDao.selectAllTraning(traningInfo);
        return new PageInfo<>(traningInfos);
    }

    /**
     * 新增
     *
     * @param traningInfo
     * @return
     */
    @Override
    public CommonResult addTraning(TraningInfo traningInfo) {
        if (null == traningInfo.getFileId()) {
            return CommonResult.response(NumberEnum.ZERO.getNum(), "参数错误");
        }
        int fileNum = fileDao.getFileNum(traningInfo.getFileId());
        if (fileNum== NumberEnum.ZERO.getNum()){
            return CommonResult.response(NumberEnum.ZERO.getNum(),"请上传附件");
        }
//        String name = traningInfoDao.name(traningInfo.getPersonId());
//        String no = traningInfoDao.selectNo(name);
//        if(StringUtils.isEmpty(no)){
//            return CommonResult.response(NumberEnum.ZERO.getNum(), "该人员未参与项目经理培训");
//        }
//        int cont = traningInfoDao.selectByCertNo(traningInfo.getCertNo());
//        if (cont != NumberEnum.ZERO.getNum()) {
//            return CommonResult.response(NumberEnum.ZERO.getNum(), "编号已被使用请重新修改");
//        }
//        if (!no.equals(traningInfo.getCertNo())) {
//            return CommonResult.response(NumberEnum.ZERO.getNum(), "编号与本人不对应");
//        }
        int num = traningInfoDao.addTraning(traningInfo);
        if (num == NumberEnum.ZERO.getNum()) {
            return CommonResult.response(num, "操作失败");
        }
        return CommonResult.response(num, "操作成功");
    }

    @Override
    public TraningInfo getInfo(String id) {
        return traningInfoDao.getInfo(id);
    }

    @Override
    public PageInfo<TraningInfo> getListByArea(Page page, TraningInfo traningInfo, String name) {
        String area = "";
        if (!Common.ADMIN.equals(name)) {
            area = corpDao.getDistinct(name);
            traningInfo.setArea(area);
        }
        //获取该登录账号能查询的培训部门
        String department = managerService.getDepartment(name);
        traningInfo.setEducationLevel(department);
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<TraningInfo> traningInfos = traningInfoDao.getListByArea(traningInfo);
        return new PageInfo<>(traningInfos);
    }

    @Override
    public PageInfo<TraningInfo> selectStateByArea(Page page, TraningInfo traningInfo, String name) {
        String area = "";
        if (StringUtils.isNotBlank(name)) {
            area = corpDao.getDistinct(name);
            traningInfo.setArea(area);
        }
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<TraningInfo> traningInfos = traningInfoDao.selectStateByArea(traningInfo);
        return new PageInfo<>(traningInfos);
    }

}
